//at_user_in_post
const attuser = {
    getData: {},
    taskConfig: { taskStatus: false },
    watchValue(key, newVal) {
        if (key == 'taskStatus' && newVal) {
            Xhs.taskConfig.taskStatus = true
        }
        if (key == 'errorTask' && newVal) {
            // 任务执行异常
            let params = {
                taskType: 'at_user_in_post',
                task_id: attuser.getData.task_id,
                taskName: '@用户引流任务',
                taskData: attuser.getData,
                errorTip: newVal,
                taskDate: Comman.getDate()
            }
            Xhs.errorTask(params)
            attuser.taskConfig.taskStatus = true
        }
    },
    init(data) {
        console.log('开始发帖,数据：', data);
        
        attuser.getData = data;
        attuser.taskConfig = Comman.defineReactive(
            {
                taskStatus: false,
                errorTask: ''
            },
            '',
            attuser.watchValue
        )
        attuser.openTask()
    },
    start(data) {
        //初始化数据
        // console.log('开始发帖,数据：',data);
        attuser.init(data)
    },
    openTask() {
        let homeMnue = btnConfig.home.homeMnue
        //重置首页
        while (!depth(homeMnue.options.depth).id(homeMnue.options.id).exists()) {
            back();
            console.log('back...');
            sleep(2000);
        }
        attuser.toVIew()
    },
    toVIew() {
        let postLink = attuser.getData.setting.post_keyword
        if (!postLink) {
            //没有帖子 直接返回
            attuser.taskConfig.taskStatus = true
            return
        }

        if (!attuser.getData.setting.xhsids) {
            // 没有回复列表 直接返回
            attuser.taskConfig.taskStatus = true
            return
        }

        if (attuser.getData.setting.xhsids && !attuser.getData.setting.xhsids.length) {
            // 没有回复列表 直接返回
            attuser.taskConfig.taskStatus = true
            return
        }
        setClip('')
        home()
        setClip(postLink)
        sleep(1000)
        Xhs.openApp()
        App.randomSleep(2, 5)
        let viewBtn = btnConfig.post.viewBtn.default()
        if (viewBtn) {
            if (viewBtn.bounds) {
                App.clickByBtn(viewBtn, true)
            } else {
                App.click(btnConfig.post.viewBtn.position.x, btnConfig.post.viewBtn.position.y, true)
            }
            attuser.toAttUser(attuser.getData.setting.xhsids)
        } else {
            // 查询不到
            attuser.taskConfig.errorTask = '剪贴板/帖子弹出出错，无法识别查看按钮'
        }
    },
    toAttUser(userList) {
        let textTip = text('当前内容无法展示').findOne(1000) || text('视频暂不可查看').findOne(1000)
        if(textTip) {
            console.log('链接无法查看，任务返回失败状态');
            getPost.taskConfig.errorTask = '链接无法查看'
        }
        let resolveInputBtn = btnConfig.post.resolveInputBtn.default()
        if (!resolveInputBtn) {
            attuser.taskConfig.errorTask = '评论框识别失败'
            return
        }
        userList.forEach((id) => {
            if (resolveInputBtn.bounds) {
                App.clickByBtn(resolveInputBtn, true)
            } else {
                App.click(btnConfig.post.resolveInputBtn.position.x, btnConfig.post.resolveInputBtn.position.y, true)
            }
            setText('')
            let attDom = className('android.widget.ImageView').drawingOrder(2).findOne(2000)
            let seedBtn = btnConfig.post.infoSendBtn.default() || btnConfig.post.infoSendBtn2.default()
            if (!attDom) {
                console.log('找不到@');
                return
            }
            if(!seedBtn){
                console.log('找不到发送按钮')
                return
            }
            App.clickByBtn(attDom, 2)
            input(id)
            sleep(2000)
            let imgBtn = className('android.view.ViewGroup').drawingOrder(1).findOne(2000)
            if (imgBtn) {
                App.clickByBtn(imgBtn, true)
                sleep(1000)
                if(!Xhs.isTest){
                    if (seedBtn.bounds) {
                        App.clickByBtn(seedBtn, 3)
                    } else {
                        App.click(btnConfig.post.infoSendBtn.position.x, btnConfig.post.infoSendBtn.position.y, 3)
                    }
                }else{
                    console.log('可以发送短信');
                    sleep(2000)
                    back()
                    sleep(2000)
                }
            }
        })
        attuser.taskConfig.taskStatus = true
    }
}
module.exports = attuser